<?php

namespace App\Admin\Controllers;

use Encore\Admin\Layout\Content;
use App\Models\Product;
use App\Models\SeckillProduct;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Controllers\AdminController;

class SeckillProductsController extends AdminController
{
    protected $title = '秒杀商品';
    
    protected function grid()
    {
        $grid = new Grid(new SeckillProduct());
	
	    $grid->disableExport();
	    
	    
        $grid->column('product_id', '商品')->display(function () {
	        return isset($this->product) ? "{$this->product->id}: {$this->product->title}" : '';
        });
        $grid->column('product_price', '商品价格')->display(function () {
            return isset($this->product->skus) ? "{$this->product->skus->first()->price_vip}" : '';
        });
        $grid->column('seckill_count', '秒杀商品数量');
        $grid->column('is_new', '是否是新人商品')->bool();
        $grid->column('is_open', '是否开启')->bool();
	    $grid->column('created_at', '创建时间')->display(function () {
	    	return $this->created_at->toDatetimeString();
	    });
        return $grid;
    }
    
    protected function detail($id)
    {
        $show = new Show(SeckillProduct::findOrFail($id));

        return $show;
    }

    
    protected function form()
    {
        $form = new Form(new SeckillProduct());
		$form->select('product_id','商品')->options(Product::all()->pluck('title', 'id'));
        $form->text('seckill_count', '秒杀商品数量')->default(1);
        $form->radio('is_new', '是否是新人商品')->default(false)->options(config('site.bool'));
        $form->radio('is_open', '是否开启')->default(true)->options(config('site.bool'));
        return $form;
    }

   
}
